import { format ,parse} from "date-fns";
enum TimeType {
  second,
  min,
  hour,
  day,
}
class TimeUtil {
  static strToDateTime(str:string){
    return parse(str, "yyyy-MM-dd HH:mm:ss", new Date());
  }
  static strToDate(str:string){
    return parse(str, "yyyy-MM-dd", new Date());
  }
  //获得两个日期之间的时间差值
  static getDayComputed(startTime: Date, endTime: Date) {
    return Math.floor((endTime.getTime() - startTime.getTime()) / (1000 * 60 * 60 * 24));
  }
  static TimeInRange(startTime: Date, endTime: Date,t:Date){
    if(startTime.getTime()>endTime.getTime()){
      return false;
    }
    let start=startTime.getTime();
    let end=endTime.getTime();
    let n=t.getTime();
    return n>=start&&n<=end;
  }

  static complute(startTime:Date,value:number,type:TimeType){
    let startM=startTime.getTime();
    value*=1000;
    if(type==TimeType.min) value*=60;
    if(type==TimeType.hour) value*=60*60;
    if(type==TimeType.day) value*=60*60*24;
    return new Date(startM+value);
  }
  static getMilliseconds(){
    return Date.now();
  }

  static toSimpleTimeString(time: Date): string {
    return format(time, "yyyy-MM-dd HH:mm:ss");
  }
  static getNowString() {
    return format(new Date(), "yyyy-MM-dd HH:mm:ss");
  }
  static getPastSeconds(oldTime:string){//单位为秒
    let oldTimeStamp = Date.parse(oldTime);
    let nowTimeStamp = Date.now();
    return (nowTimeStamp - oldTimeStamp)/1000;
  }
  static timeCmp(timeCpm: Date, timeBecmp: Date){
    return timeCpm.getTime() - timeBecmp.getTime();
  }
}
export  {TimeUtil,TimeType};
